package com.mbti.youmore.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mbti.youmore.common.exception.PostNotExistException;
import com.mbti.youmore.entity.*;
import com.mbti.youmore.mapper.*;
import com.mbti.youmore.service.FavoriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;

/**
* @author Guqier
* @description 针对表【t_favorite】的数据库操作Service实现
* @createDate 2024-05-08 20:30:45
*/
@Service
public class FavoriteServiceImpl extends ServiceImpl<FavoriteMapper, Favorite>
    implements FavoriteService{


    @Autowired
    private FavoriteMapper favoriteMapper;

    @Autowired
    private PostMapper postMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private LikeMapper likeMapper;

    @Override
    public List<Map<String, Object>> getFavorite(Integer userId) {
        List<Favorite> favorites = favoriteMapper.selectList(new LambdaQueryWrapper<Favorite>().eq(Favorite::getUserId, userId));
                List<Map<String, Object>> resultList = new ArrayList<>();

        for (Favorite favorite : favorites) {
            Map<String, Object> map = new HashMap<>();
            Post post = postMapper.selectById(favorite.getPostId());
            User user = userMapper.selectById(post.getUserId());

            map.put("postId", post.getId());
            SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            map.put("favoriteTime", dateformat.format(favorite.getTime()));
            map.put("PostTime", dateformat.format(post.getPostTime()));
            map.put("content", post.getContent());
            map.put("img", post.getImg());
            map.put("likeNum", post.getLikeNum());
            map.put("commentNum", commentMapper.selectCount(new LambdaQueryWrapper<Comment>().eq(Comment::getPostId, post.getId())));
            map.put("isLike", likeMapper.selectCount(new LambdaQueryWrapper<Like>().eq(Like::getPostId, post.getId()).eq(Like::getUserId, userId)));
            map.put("isStaring", true);
            map.put("userId", user.getUserId());
            map.put("name", user.getNickname());//发布帖子用户名
            map.put("profilePic", user.getProfilePic());
            map.put("location", post.getLocation());//帖子定位
            map.put("friendId", post.getFriendId());//艾特好友id

            resultList.add(map);
        }
        return resultList;
    }

    @Override
    @Transactional // 开启事务
    public int addFavorite(Favorite favorite) {
        //判断帖子是否存在
        Post post = postMapper.selectById(favorite.getPostId());
        if (post == null) {
            throw new PostNotExistException("帖子不存在");
        }
        //判断当前用户是否已收藏帖子
        Favorite favorite1 = favoriteMapper.selectOne(new LambdaQueryWrapper<Favorite>().eq(Favorite::getUserId, favorite.getUserId()).eq(Favorite::getPostId, favorite.getPostId()));
        if (favorite1 != null) {
            return 0;
        }
        favorite.setTime(new Timestamp(new Date().getTime()));
        return favoriteMapper.insert(favorite);
    }

    @Override
    @Transactional
    public int cancelFavorite(Integer userId, Integer postId) {
        return favoriteMapper.delete(new LambdaQueryWrapper<Favorite>().eq(Favorite::getUserId, userId).eq(Favorite::getPostId, postId));
    }

}




